package com.ssmk.module.my;


import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.AlertDialog;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.ssmk.R;
import com.ssmk.base.MVPBaseFragment;
import com.ssmk.module.home.utils.FontHelper;
import com.ssmk.module.login.Login_main_Activity;
import com.ssmk.module.my.activity.AboutMeActivity;
import com.ssmk.module.my.activity.MyCollectActivity;
import com.ssmk.module.my.activity.PersonMessageActivity;
import com.ssmk.module.my.activity.SettingActivity;
import com.ssmk.module.shoppingcar.AddressActivity;
import com.ssmk.module.shoppingcar.order.AllOrderActivity;
import com.ssmk.module.shoppingcar.order.EvaluateActivity;
import com.ssmk.utlis.EventBusMessage;
import com.ssmk.utlis.PermissionsResultListener;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareConfig;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.zhy.autolayout.AutoRelativeLayout;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.Unbinder;
import de.hdodenhof.circleimageview.CircleImageView;

import static android.content.Context.MODE_PRIVATE;


/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class MypageFragment extends MVPBaseFragment {
    @BindView(R.id.autolayout_aboutwe)
    AutoRelativeLayout autolayout_aboutwe;
    @BindView(R.id.autolayout_newaddress)
    AutoRelativeLayout autolayout_addess;
    @BindView(R.id.autola_setting)
    AutoRelativeLayout autola_seeting;
    @BindView(R.id.img_lingdang)
    ImageView mImgLingdang;

    @BindView(R.id.autulayout)
    AutoRelativeLayout mAutulayout;
    @BindView(R.id.img_qbu)
    ImageView mImgQbu;
    @BindView(R.id.img_daifahuo)
    ImageView mImgDaifahuo;
    @BindView(R.id.img_daishouhuo)
    ImageView mImgDaishouhuo;
    @BindView(R.id.img_pingjia)
    ImageView mImgPingjia;
    @BindView(R.id.img_shouhou)
    ImageView mImgShouhou;
    @BindView(R.id.img_shoucang)
    ImageView mImgShoucang;
    @BindView(R.id.img_dizhi)
    ImageView mImgDizhi;
    @BindView(R.id.img_setting)
    ImageView mImgSetting;
    @BindView(R.id.img_my)
    ImageView mImgMy;
    @BindView(R.id.img_fenxiang)
    ImageView mImgFenxiang;
    @BindView(R.id.img_dafen)
    ImageView mImgDafen;
    @BindView(R.id.autolayout_mycollect)
    AutoRelativeLayout autolayoutMycollect;
    @BindView(R.id.ar_quanbu)
    AutoRelativeLayout arQuanbu;
    @BindView(R.id.ar_daifahuo)
    AutoRelativeLayout arDaifahuo;
    @BindView(R.id.ar_daishouahuo)
    AutoRelativeLayout arDaishouahuo;
    @BindView(R.id.ar_daipingjia)
    AutoRelativeLayout arDaipingjia;
    @BindView(R.id.ar_shouhou)
    AutoRelativeLayout arShouhou;
    @BindView(R.id.shareFrends)
    AutoRelativeLayout shareFrends;
    Unbinder unbinder;
    @BindView(R.id.but_login)
    Button butLogin;
    @BindView(R.id.circimg)
    CircleImageView circimg;
    @BindView(R.id.t1)
    TextView t1;
    @BindView(R.id.t2)
    TextView t2;
    @BindView(R.id.t3)
    TextView t3;
    @BindView(R.id.t4)
    TextView t4;
    @BindView(R.id.t5)
    TextView t5;
    @BindView(R.id.tv_shoucang)
    TextView tvShoucang;
    @BindView(R.id.tv_dizhi)
    TextView tvDizhi;
    @BindView(R.id.tv_setting)
    TextView tvSetting;
    @BindView(R.id.tv_our)
    TextView tvOur;
    @BindView(R.id.tv_share)
    TextView tvShare;
    @BindView(R.id.tv_pingjia)
    TextView tvPingjia;
    Unbinder unbinder1;
    private SharedPreferences personInfo;

    String[] PERMISSIONS = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE};//权限

    @OnClick({R.id.img_lingdang, R.id.circimg, R.id.autulayout, R.id.autola_setting, R.id.but_login
            ,R.id.autolayout_aboutwe, R.id.autolayout_newaddress, R.id.autolayout_mycollect,
            R.id.ar_quanbu, R.id.ar_daifahuo, R.id.ar_daishouahuo, R.id.ar_daipingjia, R.id.ar_shouhou,
            R.id.shareFrends})
    public void onClick(View view) {
        switch (view.getId()) {
            //个人信息
            case R.id.img_lingdang:
                startActivity(new Intent(getActivity(), PersonMessageActivity.class));
                break;
            case R.id.circimg:
                startActivity(new Intent(getActivity(), PersonMessageActivity.class));
                break;
            //全部
            case R.id.ar_quanbu:
                setIntent(0);
                break;
            //待发货
            case R.id.ar_daifahuo:
                setIntent(2);
                break;
            //待收货
            case R.id.ar_daishouahuo:
                setIntent(3);
                break;
            //待评价
            case R.id.ar_daipingjia:
                startActivity(new Intent(getActivity(), EvaluateActivity.class));
                break;
            //售后
            case R.id.ar_shouhou:
                setIntent(4);
                break;
            //登录注册
            case R.id.but_login:
                Intent intent = new Intent(getActivity(), Login_main_Activity.class);
                startActivity(intent);
                break;
            //我的收藏
            case R.id.autolayout_mycollect:
                startActivity(new Intent(getActivity(), MyCollectActivity.class));
                break;
            //设置
            case R.id.autola_setting:
                startActivity(new Intent(getActivity(), SettingActivity.class));
                break;
            //关于我们
            case R.id.autolayout_aboutwe:
                startActivity(new Intent(getActivity(), AboutMeActivity.class));
                break;
            //分享
            case R.id.shareFrends:
                //分享需要获取读写的权限
                performRequestPermission("搜搜猿需要读取您的应用列表", PERMISSIONS, 0, new PermissionsResultListener() {
                    @Override
                    public void onPermissionGranted() {//表示允许权限申请
                        //当用户允许权限时，显示要分享的面板
                        new ShareAction(getActivity())
                                .setDisplayList(SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE, SHARE_MEDIA.WEIXIN,
                                        SHARE_MEDIA.SINA,SHARE_MEDIA.WEIXIN_CIRCLE)//要分享的平台
                                .setCallback(mUmShareListener)
                                .withMedia(new UMImage(getActivity(), R.drawable.umeng_socialize_qq))
                                .withText("展示的文字内容")
                                .open();

                    }

                    @Override
                    public void onPermissionDenied() {//表示拒绝权限申请
                        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                        builder.setTitle("提示")
                                .setMessage("您拒绝了授权该权限")
                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {
                                        dialogInterface.dismiss();
                                    }
                                })
                                .setCancelable(false)
                                .show();
                    }
                });
                break;
            //收货地址
            case R.id.autolayout_newaddress:
                startActivity(new Intent(getActivity(), AddressActivity.class));
                break;
        }
    }
    //跳转到订单页面
    private void setIntent(int i) {
        if (getLoginStatus()){
            Intent intent = new Intent(getActivity(), AllOrderActivity.class);
            intent.putExtra("orderState", i);
            startActivity(intent);
        }else {
            Toast.makeText(getActivity(),"您还没有登陆哦！",Toast.LENGTH_SHORT).show();
        }
    }

    //显示pop
    private void setSharePop(View view) {
        View popView = LayoutInflater.from(getActivity()).inflate(R.layout.fenxiang_popwindow, null);
        PopupWindow window = new PopupWindow(popView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置点击空白处消失
        window.setTouchable(true);
        window.setOutsideTouchable(true);
        window.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00000000")));
        // 设置背景颜色变暗
        WindowManager.LayoutParams lp = getActivity().getWindow().getAttributes();
        //lp.alpha = 0.9f;//s设置透明度
        getActivity().getWindow().setAttributes(lp);
        window.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams lp = getActivity().getWindow().getAttributes();
                lp.alpha = 1f;
                getActivity().getWindow().setAttributes(lp);
            }
        });
        window.showAtLocation(view, Gravity.BOTTOM, 0, 0);

        RadioButton pop_pengyouquan = (RadioButton) popView.findViewById(R.id.pop_pengyouquan);
        RadioButton pop_weixin = (RadioButton) popView.findViewById(R.id.pop_weixin);
        RadioButton pop_weibo = (RadioButton) popView.findViewById(R.id.pop_weibo);
        RadioButton pop_qq = (RadioButton) popView.findViewById(R.id.pop_qq);
        RadioButton pop_kongjian = (RadioButton) popView.findViewById(R.id.pop_kongjian);
        pop_qq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new ShareAction(getActivity())
                        .setDisplayList(SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE)//要分享的平台
                        .setCallback(mUmShareListener)
                        .withMedia(new UMImage(getActivity(), R.drawable.umeng_socialize_qq))
                        .withText("展示的文字内容")
                        .open();
            }
        });

    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            circimg.setVisibility(View.VISIBLE);
            butLogin.setVisibility(View.GONE);
            String iconurl = data.getStringExtra("iconurl");
            Glide.with(getActivity()).load(iconurl).into(circimg);
        }
    }

    @Override
    protected void initdata() {

    }
    @Override
    protected void initView() {

        boolean loginStatus = getLoginStatus();//得到login状态
        setViewVisibilitySataus(loginStatus);

        setTextWay();//设置字体

        UMShareConfig config = new UMShareConfig();
        config.isOpenShareEditActivity(true);//分享
        UMShareAPI.get(getActivity()).setShareConfig(config);
    }

    //设置字体
    private void setTextWay() {
        FontHelper.applyFont(getActivity(), t1, 1);
        FontHelper.applyFont(getActivity(), t2, 1);
        FontHelper.applyFont(getActivity(), t3, 1);
        FontHelper.applyFont(getActivity(), t4, 1);
        FontHelper.applyFont(getActivity(), t5, 1);
        FontHelper.applyFont(getActivity(), tvShoucang, 5);
        FontHelper.applyFont(getActivity(), tvDizhi, 5);
        FontHelper.applyFont(getActivity(), tvSetting, 5);
        FontHelper.applyFont(getActivity(), tvShare, 5);
        FontHelper.applyFont(getActivity(), tvOur, 5);
        FontHelper.applyFont(getActivity(), tvPingjia, 5);
        FontHelper.applyFont(getActivity(), butLogin, 1);
    }

    @Override
    public void onResume() {
        super.onResume();

        boolean loginStatus = getLoginStatus();//得到login状态
        setViewVisibilitySataus(loginStatus);
    }
    @Override
    protected int layoutRes() {
        return R.layout.fragment_my_page;
    }
    @Override
    public void UpData(Object o) {

    }

    private boolean getLoginStatus(){
        //得到login状态
        personInfo = getActivity().getSharedPreferences("personInfo", MODE_PRIVATE);
        boolean loginState = personInfo.getBoolean("loginState", false);

        return loginState;
    }

    //设置登录与未登录状态下View的隐藏与显示
    private void setViewVisibilitySataus(boolean loginState){
        if (personInfo != null){
            String iconurl = personInfo.getString("iconurl", null);
            if (loginState) {
                circimg.setVisibility(View.VISIBLE);
                butLogin.setVisibility(View.GONE);
                if (iconurl==null){
                    circimg.setImageResource(R.mipmap.ic_launcher);
                }else {
                    Glide.with(getActivity()).load(iconurl).into(circimg);
                }
            } else {
                circimg.setVisibility(View.GONE);
                butLogin.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void MyEventData(EventBusMessage t) {
        super.MyEventData(t);
        String message = (String) t.getMessage();
        circimg.setVisibility(View.VISIBLE);
        butLogin.setVisibility(View.GONE);
        Glide.with(getActivity()).load(message).into(circimg);
    }

    private UMShareListener mUmShareListener = new UMShareListener(){
        @Override
        public void onStart(SHARE_MEDIA share_media) {
            Toast.makeText(getActivity(),"启动分享！",Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onResult(SHARE_MEDIA share_media) {
            Toast.makeText(getActivity(),"分享成功！",Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(SHARE_MEDIA share_media, Throwable throwable) {
            if (throwable.getMessage().equals("2008")){
                Toast.makeText(getActivity(),"请安装该应用！",Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onCancel(SHARE_MEDIA share_media) {
            Toast.makeText(getActivity(),"取消分享！",Toast.LENGTH_SHORT).show();
        }
    };
}
